<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="${params.contextPath}/common/webupload/webuploader.css"/>
<script type="text/javascript" src="${params.contextPath}/common/webupload/webuploader.js"></script>
<script type="text/javascript">
    var _uploader_init = {
        url:"${params.contextPath}/web/attachment/upload.json",
        renderId:"#select-file-button",
        renderLabel:"",
        //fileNumLimit:3,
        fileSingleSizeLimit:500 * 1024 * 1024,//500M
        //accept:null,
        formData:{},//自定义参数，初始化后用uploader.options.formData._t = new Date().getTime(); 调用
        remove:true,
        beforeFileQueued:function(uploader, file){return true;},
        uploadSuccess:function(file, data){},
        error:function (type) {
            $.message(type)
        }
    };

    var setHeader = function(object, data, headers) {
        var isIE = !!window.ActiveXObject;
        if (isIE) {
            headers['Access-Control-Allow-Origin'] = '*';
            headers['Access-Control-Request-Headers'] = 'content-type';
            headers['Access-Control-Request-Method'] = 'POST';
        }
    };

    $.upload = function (_init_param_json) {
        var isIE = !!window.ActiveXObject;
        if (!WebUploader.Uploader.support("flash") && isIE) {
            layer.confirm("当前浏览器的flash版本过旧，请点击如下链接下载并安装<br/><a target='_blank' href='${params.contextPath}/resource/flashplayer_23_ax_debug_23.0.0.185.exe'>点击下载兼容的flash版本</a>", function(){
                window.location.href = "${params.contextPath}/resource/flashplayer_23_ax_debug_23.0.0.185.exe";
            });
        }
        if (_init_param_json) {
            $.extend(_uploader_init, _init_param_json);
        }
        var _uploader = WebUploader.create({
            swf:'${params.contextPath}/common/webupload/Uploader.swf',
            server:_uploader_init.url,
            pick:{
                id:_uploader_init.renderId,
                label:_uploader_init.renderLabel
            },
            fileVal:"files",
            resize:false,
            auto:true,
            fileNumLimit:_uploader_init.fileNumLimit,
            //runtimeOrder:'flash',
            fileSingleSizeLimit:_uploader_init.fileSingleSizeLimit,
            accept:_uploader_init.accept
            /* accept:{
                title:'Images',
                extensions:'gif,jpg,jpeg,bmp,png',
                mimeTypes:'image/*'
            } */
        });
        _uploader.on('uploadBeforeSend ', setHeader);
        _uploader.on("beforeFileQueued", function(file){
            if (file.size <= 0) {
                $.message("该上传文件是空文件，无法上传");
                return false;
            }
            if (_uploader_init.fileSingleSizeLimit &&
                    file.size > _uploader_init.fileSingleSizeLimit) {
                $.message("该上传文件的大小超过"+(_uploader_init.fileSingleSizeLimit/(1024*1024))+"M");
                return false;
            }
            /*var array = file.name.split(".");
            var lastName = array[array.length - 1].toUpperCase();
            var lastNameArray = ['HTML', 'JS', 'EXE', 'JSP', 'JAR'];
            if (lastNameArray.indexOf(lastName) >= 0) {
                layer.msg("上传图片的格式非法");
                return false;
            }*/
            var params = _uploader_init.formData;
            for (var key in  params) {
                _uploader.options.formData[key] = params[key];
            }
            //alert(JSON.stringify(_uploader.options.formData));
            var isSuccess = _uploader_init.beforeFileQueued(_uploader, file);
            /*if (isSuccess && $(".op-upload-loading").length <= 0) {
                $(_uploader_init.renderId).after("<img src='${resourceUrl!}plugins/layer/skin/default/loading-1.gif' class='op-upload-loading' style='width:30px;margin-left:10px;'/>");
            }*/
            if (isSuccess) {
                $.loading();
            }
            return isSuccess;
        });
        _uploader.on("uploadSuccess", function(file, data){
            var files = _uploader.getFiles("progress");
            if (files.length <= 0) {
                //$(".op-upload-loading").remove();
                $.closeLoading();
            }
            if (_uploader_init.remove) {
                _uploader.removeFile(file, true);
            }
            _uploader_init.uploadSuccess(file, data);
        });
        _uploader.on("error", function(type){
            _uploader_init.error(type);
        });
    };
</script>